const defaultState = {
    feeList: [],
    homeFee: 0,
    sheetFee: 0,
    currency: undefined,
    sheetCurrency: undefined
};
export default function clientFee(state = defaultState, action) {
    const {type, payload} = action;
    switch (type) {
        case 'CLIENT.UPDATE_CLIENT_FEE':
            if (payload && payload.length > 0) {
                state.feeList.forEach(fee => {
                    const found = payload.find(f => f.feeName === fee.feeName);
                    if (found) {
                        fee.feeName = found.feeName;
                        fee.feeCount = found.feeCount;
                        fee.currency = found.currency;
                    }
                });
            }
            const fees = [];
            state.feeList.forEach(fee => {
                fees.push({...fee, id: Math.random().toString()});
            });
            return {...state, feeList: fees};

        case 'CLIENT.GET_FEE_LIST':
            const feeList = [];
            if (payload && payload.length > 0) {
                payload.forEach(item => feeList.push({
                    id: Math.random().toString(),
                    feeName: item.name,
                    feeCount: 0,
                    currency: undefined
                }));
            }
            return {...state, feeList: [...feeList]};
        case 'CLIENT.UPDATE_FEE_LIST':
            return {...state, feeList: [...payload]};
        case 'CLIENT.UPDATE_BAOGUAN_FEE':
            return {
                ...state,
                homeFee: payload.homeFee,
                sheetFee: payload.sheetFee,
                currency: payload.currency,
                sheetCurrency: payload.sheetCurrency
            };
        case 'CLIENT.UPDATE_CURRENCY':
            return {...state, ...payload};
        case 'CLIENT.HIDE_CREATE':
        case 'CLIENT.HIDE_MODIFY':
            return {
                ...state,
                homeFee: 0,
                sheetFee: 0,
                currency: undefined,
                sheetCurrency: undefined
            };
        default:
            return state;
    }
}

